package com.paic.mhis.hcpms.agreement.healthcareprovider.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.hcpms.agreement.healthcareprovider.biz.service.DiagnosisService;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.Constants;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HttpProcessUtil;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.ValidatorUtil;
import com.paic.pafa.appclient.annotation.BizServicesProxy;
import com.paic.pafa.web.BaseRest;

@Controller
public class DiagnosisController extends BaseRest {
	public void setDiagnosisService(DiagnosisService diagnosisService) {
		this.diagnosisService = diagnosisService;
	}

	@BizServicesProxy
	private DiagnosisService diagnosisService;

	/**
	 * 诊疗项目新增
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/diagnosis/create", method = RequestMethod.POST)
	public Map<String, Object> addDiagnosis(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			// assembleTransferJson(request, transferJson);
			result = this.diagnosisService.addDiagnosis(transferJson);

		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 诊疗项目修改
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/diagnosis/update", method = RequestMethod.POST)
	public Map<String, Object> updateDiagnosis(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			// assembleTransferJsonUpdate(request, transferJson);
			result = this.diagnosisService.updateDiagnosis(transferJson);

		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 诊疗项目信息批量删除
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/diagnosis/delete", method = RequestMethod.POST)
	public Map<String, Object> deleteDiagnosis(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			// assembleTransferJsonUpdate(request, transferJson);
			result = this.diagnosisService.deleteDiagnosis(transferJson);

		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 诊疗项目列表查询
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/diagnosis/page")
	public Map<String, Object> queryDiagnosisList(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String providerid = request.getParameter("providerid");
			String treatCode = request.getParameter("treatCode");
			String name = request.getParameter("name");
			String page = request.getParameter("page");
			String pageSize = request.getParameter("pageSize");
			if (!isEmpty(page) || !isEmpty(pageSize)) {
				if (!ValidatorUtil.isPlusInteger(page)
						|| !ValidatorUtil.isPlusInteger(pageSize)) {
					result = new HashMap<String, Object>();
					HttpProcessUtil.generateExceptionResult(result,
							Constants.STATE_KEY,
							Constants.FAILED_VALIDATE_STATE_VALUE,
							Constants.MSG_KEY,
							Constants.PAGE_VALIDATE_FAILED_MSG);
					return result;
				}
			}
			JSONObject transferJson = new JSONObject();
			transferJson.put("page", page);
			transferJson.put("pageSize", pageSize);
			transferJson.put("providerid", providerid);
			transferJson.put("treatCode", treatCode);
			transferJson.put("name", name);
			result = this.diagnosisService.queryDiagnosisList(transferJson);

		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 诊疗项目详情查询
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/diagnosis/show", method = RequestMethod.GET)
	public Map<String, Object> queryDiagnosisDetail(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String id = request.getParameter("id");
			JSONObject transferJson = new JSONObject();
			transferJson.put("id", id);
			result = diagnosisService.queryDiagnosisDetail(transferJson);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	private boolean isEmpty(String param) {
		return param == null || param.equals("");
	}
}
